<template>
	<view>
		<!-- 头部搜索框 -->
		<search :placeholder="$t('homepage.CallDeal.machineCode')" />
		<view class="formList">
			<zy-forms :model="formList" border labelWidth="120">
				<zy-form-item :label="$t('homepage.CallDeal.callCode')+$t('public.colon')"><text
						class="formText">{{formList.code || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('homepage.CallDeal.machineName')+$t('public.colon')"><text
						class="formText">{{formList.name || $t('public.null')}}</text>
				</zy-form-item>
				<zy-form-item :label="$t('homepage.CallDeal.failureReason')+$t('public.colon')">
					<zy-input v-model="formList.failureReason"
						:placeholder="$t('public.pleaseInput')+$t('homepage.CallDeal.failureReason')" />
				</zy-form-item>
				<zy-form-item :label="$t('homepage.CallDeal.resolve')+$t('public.colon')">
					<zy-input v-model="formList.remarks"
						:placeholder="$t('public.pleaseInput')+$t('homepage.CallDeal.resolve')" />
				</zy-form-item>
			</zy-forms>
		</view>
		<!-- 底部按钮 -->
		<bottomBtn fixed :leftText="$t('public.reset')" :rightText="$t('public.submit')" @rightBtn="submitBtn"
			@leftBtn="resetBtn" :leftDisabled="disabled" :rightDisabled="disabled">
		</bottomBtn>
	</view>
</template>

<script>
	import {
		handleQuestion,
		machineGetMapyByField
	} from "@/api/homepage.js"
	export default {
		data() {
			return {
				id: '',
				formList: {
					id: '',
					code: '',
					name: '',
					failureReason: '',
					remarks: '',
				},
				disabled: true,
			}
		},
		onLoad(options) {
			this.id = options.id
			//PDA扫描
			var _this = this
			uni.$on('scancodedate', function(data) {
				_this.searchList(data.code.replace(/\s/g, ""))
			})
		},
		onUnload() {
			// 移除监听事件      
			uni.$off('scancodedate')
		},
		methods: {
			searchList: function(e) {
				machineGetMapyByField({
					fieldValue: e,
					fieldName: 'code'
				}).then((res) => {
					if (res.code == 0) {
						this.formList = res.data
						this.disabled = false
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				})
			},
			resetBtn() {
				this.formList.deptId = ''
				this.formList.code = ''
				this.formList.name = ''
				this.formList.failureReason = ''
				this.formList.remarks = ''
			},
			submitBtn() {
				handleQuestion({
					id: this.id,
					machineId: this.formList.id,
					failureReason: this.formList.failureReason,
					remarks: this.formList.remarks
				}).then((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
					uni.$emit('callDeal')
					uni.navigateBack({
						delta: 1
					});
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.formList {
		background-color: #fff;
		margin: 20rpx;
		border-radius: 10rpx;
		padding-bottom: 10rpx;
		margin-bottom: 110rpx;
	}
</style>